import pymysql
class SqlUtils:
    def __init__(self,host,port,user,passwd,db,charset):
        self.host = host
        self.port = port
        self.user = user
        self.passwd = passwd
        self.db = db
        self.charset = charset
    def createcon(self):
        self.con = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.passwd, db=self.db, charset=self.charset)
        self.cur = self.con.cursor()
    def closecon(self):
        self.con.commit()
        self.con.close()
    def updateInfo(self,sql,l=None):
        self.createcon()
        try:
            r = self.cur.execute(sql, l)
            return r
        except Exception as result :
            print("出现异常了，内容是：%s"%result)
        finally:
            self.closecon()
    def addInfo(self,sql,l=None):
        self.createcon()
        try:
            r = self.cur.execute(sql,l)
            return r
        except Exception as result :
            print("出现异常了，内容是：%s"%result)
        finally:
            self.closecon()
    def delStu(self,sql,l=None):
        self.createcon()
        try:
            r = self.cur.execute(sql,l)
            return r
        except Exception as result :
            print("出现异常了，内容是：%s"%result)
        finally:
            self.closecon()
    def selectAll(self,sql,l=None):
        self.createcon()
        try:
            self.cur.execute(sql,l)
            r = self.cur.fetchall()
            return r#出来的是数据，不是数字
        except Exception as e :
            print("出现异常了，内容是：%s"%e)
        finally:
            self.closecon()
    def selectOne(self,sql,l=None):
        self.createcon()
        try:
            self.cur.execute(sql,l)
            r = self.cur.fetchone()
            return r
        except Exception as e :
            print("出现异常了，内容是：%s"%e)
        finally:
            self.closecon()
    def selectMany(self,sql,l=None,num=1):
        self.createcon()
        try:
            self.cur.execute(sql,l)
            r = self.cur.fetchmany(num)
            return r
        except Exception as result :
            print("出现异常了，内容是：%s"%result)
        finally:
            self.closecon()